# 保存名片信息
card_list = []


def show_menu():
    """显示菜单"""
    print("*" * 50)
    print("欢迎使用【名片管理系统】V1.0")
    print("")
    print("1. 新增名片")
    print("2. 显示全部")
    print("3. 搜索名片")
    print("")
    print("0. 退出系统")
    print("*" * 50)


def new_card():
    """新增名片"""
    print("-" * 50)
    # 提示用户输入信息
    card_name = input("请输入姓名")
    card_phone = input("请输入电话")
    card_qq = input("请输入QQ")
    card_email = input("请输入email")
    # 构建字典
    card_dict = {"name": card_name,
                 "phone": card_phone,
                 "qq": card_qq,
                 "email": card_email}
    # 将字典插入列表
    card_list.append(card_dict)
    # 提示保存成功
    print(card_list)
    print("%s 信息保存成功" % card_name)


def show_all():
    """显示所有名片"""
    print("-" * 50)
    # 判断是否存在名片记录，如果不存在则提示
    if len(card_list) == 0:
        print("没有名片，请添加名片")
        return

    # 打印表头
    for head in ["姓名", "电话", "qq", "邮箱"]:
        print(head, end="\t\t")
    print("")
    print("=" * 50)
    # 遍历名片列表输出信息
    for card in card_list:
        print("%s\t\t%s\t\t%s\t%s" % (card["name"],
                                      card["phone"],
                                      card["qq"],
                                      card["email"]))


def search_card():
    """搜索名片"""
    print("-" * 50)
    find_name = input("请输入姓名")
    # 根据输入的姓名查找名片
    for card in card_list:
        if card["name"] == find_name:
            print("姓名\t\t电话\t\tQQ\temail")
            print("%s\t%s\t%s\t%s" % (card["name"],
                                      card["phone"],
                                      card["qq"],
                                      card["email"]))
            handle_card(card)
            break
    else:
        print("没有找到%s的信息" % find_name)


def handle_card(find_card):
    """处理名片信息"""
    input_str = input("请输入要进行的操作"
                      "[1]修改 [2]删除 [0]返回上一级")
    if input_str == "1":
        find_card["name"] = input_cart_info(find_card["name"], "姓名：")
        find_card["phone"] = input_cart_info(find_card["phone"], "电话：")
        find_card["QQ"] = input_cart_info(find_card["qq"], "QQ：")
        find_card["email"] = input_cart_info(find_card["email"], "email：")
        print("修改名片成功")
    elif input_str == "2":
        card_list.remove(find_card)


def input_cart_info(card_value, tip_msg):
    """如果直接输入回车返回原有值，如果输入内容则返回输入的内容
    :param card_value: 原有值
    :param tip_msg: 输入提示
    :return: 修改的内容
    """

    input_info = input(tip_msg)
    if len(input_info) > 0:
        # 输入新内容
        return input_info
    else:
        return card_value
